<template>
  <el-dialog
    title="新增员工"
    :visible.sync="dialogVisible"
    width="50%"
    @close="closeEvent"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username" placeholder="1-30个字符" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="form.timeOfEntry"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment" placeholder="请选择">
          <el-option
            v-for="item in employeesData.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="form.workNumber" placeholder="1-20个字符" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="form.departmentName"
          placeholder="1-30个字符"
          readonly
          @focus="focusEvent"
        />
        <!-- 树形控件 -->
        <el-tree
          v-if="isShow"
          :loading="loading"
          :data="list"
          :props="{ label: 'name' }"
          @node-click="nodeClick"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="form.correctionTime"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align: center">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import employeesData from '@/api/constant/employees' // 导入过来的数据不能直接用,要在data中定义
import { companydepartment } from '@/api/departments'
import { treeData } from '@/utils/index' // 属性数据转换方法
import { regPhone } from '@/utils/validate'
import { sysUserPost } from '@/api/employees'
export default {
  data() {
    return {
      dialogVisible: false,
      employeesData,
      isShow: false,
      loading: true, // 加载属性
      list: [],
      form: {
        username: '', //	string	非必须		姓名
        mobile: '', //	string	非必须		手机号
        timeOfEntry: '', //	string	非必须		入职时间
        formOfEmployment: 1, //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //	string	非必须		组织名称
        correctionTime: '' //	string	非必须		转正时间
      },
      rules: {
        username: [
          { required: true, message: '必填', trigger: 'blur' },
          { min: 1, max: 30, message: '输入1-30个字符', trigger: 'blur' }
        ], //	string	非必须		姓名
        mobile: [
          { required: true, message: '必填', trigger: 'blur' },
          {
            validator(rule, val, callback) {
              regPhone(val) ? callback() : callback(new Error('输入正确手机号'))
            },
            trigger: 'blur'
          }
        ], //	string	非必须		手机号
        timeOfEntry: [{ required: true, message: '必填', trigger: 'blur' }], //	string	非必须		入职时间
        formOfEmployment: [
          { required: true, message: '必填', trigger: 'blur' }
        ], //	number	非必须		聘用形式
        workNumber: [
          { required: true, message: '必填', trigger: 'blur' },
          { min: 1, max: 20, message: '输入1-20个字符', trigger: 'blur' }
        ], //	string	非必须		工号
        departmentName: [
          { required: true, message: '必填', trigger: 'blur' },
          { min: 1, max: 30, message: '输入1-30个字符', trigger: 'change' }
        ], //	string	非必须		组织名称
        correctionTime: [{ required: true, message: '必填', trigger: 'blur' }] //	string	非必须		转正时间
      }
    }
  },
  methods: {
    async focusEvent() {
      // input 聚焦
      this.isShow = true
      const { data } = await companydepartment()
      this.loading = false
      this.list = treeData(data.depts)
    },
    nodeClick(row) {
      // 树形组件点击行
      this.form.departmentName = row.name
      this.isShow = false
    },
    submit() {
      this.$refs.form.validate(async(val) => {
        if (val) {
          await sysUserPost(this.form)
          this.$message.success('添加成功')
          this.$parent.getData()
          this.dialogVisible = false
        }
      })
    },
    closeEvent() {
      this.$refs.form.resetFields()
      this.form = {
        username: '', //	string	非必须		姓名
        mobile: '', //	string	非必须		手机号
        timeOfEntry: '', //	string	非必须		入职时间
        formOfEmployment: 1, //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //	string	非必须		组织名称
        correctionTime: '' //	string	非必须		转正时间
      }
    }
  }
}
</script>

<style></style>
